package com.blue.controller.workflow;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blue.common.annotation.NotNull;
import com.blue.common.constant.CommonConstant;
import com.blue.common.entity.Relation;
import com.blue.common.utils.ThreadLocalUtils;
import com.blue.vo.workflow.NodeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.utils.QueryWrapperUtils;
import org.springframework.web.bind.annotation.*;
import com.blue.common.utils.ConvertUtils;
import com.blue.po.workflow.InstancePO;
import com.blue.dto.workflow.InstanceDTO;
import com.blue.service.workflow.IInstanceService;
import com.blue.common.entity.ResponseResult;

import java.util.Map;


/**
 * 流程实例表
 *
 * @author nanjie.chen
 * @email 15696471158@163.com
 * @date 2025-03-07
 */
@RestController
@RequestMapping("/workflow/instance")
@Tag(name = "流程实例表")
public class InstanceController {
    @Autowired
    private IInstanceService instanceService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    public ResponseResult<?> list(InstanceDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        InstancePO instancePO = ConvertUtils.convert(InstancePO.class, requestDTO);
        String userId = ThreadLocalUtils.getLoginUserInfo().getUserId();
        instancePO.setInitiatorId(userId);
        Relation relation1 = new Relation("definition_nm", "workflow_definition t1 on t.definition_id = t1.id");
        MPJQueryWrapper<InstancePO> queryWrapper = QueryWrapperUtils.initQueryWrapper(instancePO, "t.id,t.definition_id,t.status,t.start_time,t.end_time", "t.start_time", CommonConstant.DESC, relation1);
        queryWrapper.eq(StringUtils.isNotBlank(requestDTO.getWfNo()),"t1.wf_no", requestDTO.getWfNo());
        Page<InstancePO> page = new Page<>(pageNum, pageSize);
        Page<InstancePO> pageResult = instanceService.page(page, queryWrapper);
        instanceService.computeTime(pageResult.getRecords());
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/getProcessInfo")
    @Operation(summary = "获取流程信息")
    @NotNull(value = "id", message = "流程实例ID不能为空！")
    public ResponseResult<?> instanceStatus(@RequestBody InstanceDTO requestDTO) {
        Map<String,String> resultMap =  instanceService.getProcessInfo(requestDTO.getId());
        return ResponseResult.okResult(resultMap);
    }

    @PostMapping("/getNodeInfo")
    @Operation(summary = "获取节点信息")
    @NotNull(value = "id", message = "流程实例ID不能为空！")
    public ResponseResult<?> getNodeInfo(@RequestBody InstanceDTO requestDTO) {
        NodeVO nodeInfo = instanceService.getNodeInfo(requestDTO.getId());
        return ResponseResult.okResult(nodeInfo);
    }
}
